<!--
    JBoss, Home of Professional Open Source
    Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual
    contributors by the @authors tag. See the copyright.txt in the
    distribution for a full listing of individual contributors.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head>
    <title>tasks-jsf</title>

    <h:outputStylesheet>
        h1 {
            float: left;
        }
        div.clear {
            clear: both;
        }
        .loginDetails {
            float: right;
        }
        .taskGrid {
            width: 100%;
        }
        .taskGrid td:first-child {
            width: 30%;
        }
        .taskGrid td {
            vertical-align: top
        }
    </h:outputStylesheet>
</h:head>

<h:body>
    <h1>Tasks</h1>


    <h:form>
        <div class="loginDetails">
            #{currentUser.username} (
            <h:commandLink value="logout" action="#{authController.logout}" />
            )
        </div>
    </h:form>

    <div class="clear" />

    <h:panelGroup rendered="#{authController.logged}">
        <h:outputScript>
            function onAddTask(data) {
                if (data.status == 'success') {
                    var taskTitleInput = document.getElementById('form:taskTitle');
                    taskTitleInput.value = '';
                    taskTitleInput.focus();
                }
            }
        </h:outputScript>

        <h:form id="form">
            <fieldset>
                <legend>Add Task</legend>
                <h:panelGrid columns="3">
                    <h:outputLabel value="Task title" />
                    <h:inputText id="taskTitle" value="#{taskTitle}" />

                    <h:commandButton value="Add task" action="#{taskController.createTask(taskTitle)}">
                        <f:ajax execute="@form" render=":form:taskGrid" onevent="onAddTask" />
                    </h:commandButton>
                </h:panelGrid>
            </fieldset>

            <h:panelGrid id="taskGrid" styleClass="taskGrid" columns="2">
                <h:panelGroup id="taskList">
                    <fieldset>
                        <legend>List</legend>
                        <h:dataTable var="task" value="#{taskList.all}">
                            <h:column>
                                <h:commandLink action="#{taskController.deleteTask(taskForDeletion)}">
                                    <h:selectBooleanCheckbox />
                                    <f:setPropertyActionListener target="#{taskForDeletion}" value="#{task}" />
                                    <f:ajax render=":form:taskGrid" />
                                </h:commandLink>

                                <h:commandLink value="#{task.title}">
                                    <f:setPropertyActionListener target="#{taskController.currentTask}" value="#{task}" />
                                    <f:ajax render=":form:taskDetail" />
                                </h:commandLink>
                            </h:column>
                        </h:dataTable>
                    </fieldset>
                </h:panelGroup>

                <h:panelGroup id="taskDetail">
                    <fieldset>
                        <legend>Detail</legend>
                        <h:panelGroup>
                            <p>
                                <h:outputText value="#{currentTask.title}" rendered="#{not empty currentTask}" />
                            </p>
                            <p>
                                <h:commandLink value="Complete Task" action="#{taskController.deleteCurrentTask()}"
                                    style="display: #{empty currentTask ? 'none' : 'inline'}">
                                    <f:ajax render=":form:taskGrid" />
                                </h:commandLink>
                            </p>
                        </h:panelGroup>
                    </fieldset>
                </h:panelGroup>
            </h:panelGrid>
        </h:form>
    </h:panelGroup>

</h:body>

</html>